<?php

namespace app\admin\controller\litestore;

use app\common\controller\Backend;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Litestoreorder extends Backend
{
    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = ['searchcode'];

    /**
     * Litestoreorder模型对象
     * @var \app\admin\model\litestore\Litestoreorder
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\litestore\Litestoreorder;
        $this->view->assign("payStatusList", $this->model->getPayStatusList());
        $this->view->assign("freightStatusList", $this->model->getFreightStatusList());
        $this->view->assign("receiptStatusList", $this->model->getReceiptStatusList());
        $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['user'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['user'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {
                $row->visible(['id', 'order_no', 'total_price', 'pay_price', 'pay_time', 'express_price', 'freight_time', 'receipt_time', 'order_status', 'updatetime', 'createtime', 'delivery_method']);
//                $row->visible(['address']);
//                $row->getRelation('address')->visible(['name']);
                $row->visible(['user']);
                $row->getRelation('user')->visible(['nickname', 'mobile']);
            }
            $list = collection($list)->toArray();

            //实付金额
            $pay_amount = $this->model->with(['user'])->where($where)->where(['pay_status' => 20, 'order_status' => ['<>', 20]])->sum('pay_price');

            $result = array("total" => $total, "rows" => $list, 'extend' => ['pay_amount' => (float)$pay_amount]);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function detail()
    {
        if ($this->request->isPost()) {
            $id = input('post.ids');
            $row = $this->model->get($id);
            $row['freight_status'] = "20";
            $row['freight_time'] = time();
            $row['express_company'] = input('post.virtual_name');
            $row['express_no'] = input('post.virtual_sn');
            $row->save();
            $this->success();
        }

        $param = $this->request->param();
        $row = $this->model->get($param['ids']);
        $this->view->assign('vo', $row);
        return $this->view->fetch();
    }

    public function verify()
    {
        if ($this->request->isPost()) {
            $code = $this->request->post('code', '');
            if (empty($code)) {
                $this->error(__('Invalid parameters'));
            }

            $order_info = $this->model->with(['user', 'goods'])->where(['redeem_code' => $code, 'pay_status' => 20])->find();

            if (empty($order_info)) {
                $this->error(__('No Results were found'));
            }

            if ($order_info->order_status == 30) {
                $this->error(__('核销码已被使用'));
            }

            $order_info->save([
                'receipt_status' => '20',
                'receipt_time' => time(),
                'order_status' => '30',
                'freight_status' => '20'
            ]);

            (new \app\admin\model\litestore\Verify())->save([
                'order_id' => $order_info->id,
                'redeem_code' => $order_info->redeem_code,
                'verifier_id' => $this->auth->id,
                'verifier_name' => $this->auth->username,
            ]);

            $this->success();
        }

        return $this->view->fetch();
    }

    /**
     *
     * @internal
     */
    public function searchcode()
    {
        $code = $this->request->post('code', '');
        if (empty($code)) {
            $this->error(__('Invalid parameters'));
        }

        $order_info = $this->model->with(['user', 'goods'])->where(['redeem_code' => $code, 'pay_status' => 20])->find();

        if (empty($order_info)) {
            $this->error(__('No Results were found'));
        }

        $order_info->createtime = date('Y-m-d H:i:s', $order_info->createtime);

        $this->success('', '', $order_info);
    }
}
